const formatNumber = n => {
        n = n.toString()
        return n[1] ? n : '0' + n
}

const formatTime = time => {
        var date = new Date(time);
        var year = date.getFullYear()
        var month = date.getMonth() + 1
        var day = date.getDate()
        return [year, month, day].map(formatNumber).join('-')
}
/**
 * 时间戳转化为年 月 日 时 分 秒
 * ts: 传入时间戳
 * format：返回格式，支持自定义，但参数必须与formateArr里保持一致
 */
function tsFormatTime(timestamp, format) {

        const formateArr = ['Y', 'M', 'D', 'h', 'm', 's'];
        let returnArr = [];

        let date = new Date(timestamp);
        let year = date.getFullYear()
        let month = date.getMonth() + 1
        let day = date.getDate()
        let hour = date.getHours()
        let minute = date.getMinutes()
        let second = date.getSeconds()
        returnArr.push(year, month, day, hour, minute, second);

        returnArr = returnArr.map(formatNumber);

        if (format === undefined || format == "") {
                format = "Y-M-D h:m:s";
        }

        for (var i in returnArr) {
                format = format.replace(formateArr[i], returnArr[i]);
        }
        return format;

}
/**
 * startWith
 */
function startWith(str, seg_str) {
        if (seg_str == null || seg_str == "" || seg_str.length == 0 || seg_str.length > str.length) {
                return false
        }
        if (str.substr(0, seg_str.length) == seg_str) {
                return true
        } else {
                return false
        }
}

function endWith(str, seg_str) {
        if (seg_str == null || seg_str == "" || seg_str.length == 0 || seg_str.length > str.length)
                return false
        if (str.substr(str.length - seg_str.length) == seg_str)
                return true
        else
                return false
}

function abbr(str, n) {
  var name = str;
  if (str.length > n) {
    if (endWith(str, '大学')) {
      name = str.replace('大学', '');
    } else if (endWith(str, '学院')) {
      name = str.replace('学院', '');
    }
    name = name.slice(0, n) + '..';
  }
  return name
}

function time2Human(dateTimeStr) {
  var dateTimeStr = dateTimeStr.replace(/-/g, '/');
  //console.log('date ->', dateTimeStr)
  var date = new Date(dateTimeStr).getTime();
  var tt = new Date(date);
  var days = parseInt((new Date().getTime() - date) / 86400000);
  var today = new Date().getDate();
  var year = tt.getFullYear();
  var mouth = tt.getMonth() + 1;
  var day = tt.getDate();
  var time = tt.getHours() < 10 ? "0" + tt.getHours() : tt.getHours();
  var min = tt.getMinutes() < 10 ? "0" + tt.getMinutes() : tt.getMinutes();
  var result, offset;
  　　　　　　　offset = Math.abs(today - day);
  　　　　if (days < 4 && offset < 4) {
    　　　　　　　if (offset === 0) {
      result = "今天" + time + ":" + min;
    } else if (offset === 1) {
      result = "昨天" + time + ":" + min;
    } else if (offset === 2) {
      result = "前天" + time + ":" + min;
    }
  } else {
    result = year + "-" + mouth + "-" + day + " " + time + ":" + min;
  }
  return result
}

//export 
module.exports = {
        tsFormatTime: tsFormatTime,
        startWith: startWith,
        endWith: endWith,
        formatTime: formatTime,
        abbr: abbr,
  time2Human: time2Human,
}